--[[--------------------------------------------------------------------
AntiSocial by Zariel / Kanne @ Trollbane.eu
	ToDo:
			Add message log for blocked friends?
	Chatfilter based on ShutUp by Rabbit http://svn.wowace.com/wowace/trunk/ShutUp/
-----------------------------------------------------------------------]]
local AntiSocial = DongleStub("Dongle-1.1"):New("AntiSocial")
local friends = {}
local warned = {}
local databasedefaults = {
	profile = {
		["silent"] = false,
		["enabled"] = true,
		["friends"] = false,
		["message"] = "Im AntiSocial, go away.",
		blocks = {	
			["invite"]=true, 
			["trade"]=true,
			["whisper"]=true,
			["duel"]=true,
		},
	}
}

function AntiSocial:PrintAbout()
	self:Print("For when you want to be alone.")
end

function AntiSocial:Initialize ()
	self.db = self:InitializeDB("AntiSocialDB", databasedefaults)
	self.slashcmd = self:InitializeSlashCommand("AntiSocial Slash Commands","ANTISOCIAL", "as", "antisocial")
	friends[UnitName("Player")] = true
end

function AntiSocial:Disable ()
end

function AntiSocial:Enable ()
	self.slashcmd:RegisterSlashHandler( "About: about this addon.", "^about$","PrintAbout")
	self.slashcmd:RegisterSlashHandler( "Friend: toggles blocking friends", "^friend$","BlockFriends")
	self.slashcmd:RegisterSlashHandler( "Trade: toggles blocking trades", "^trade$","TradeSw")
	self.slashcmd:RegisterSlashHandler( "Enable: Enable of Disable the addon", "^enable$","EnableSw")
	self.slashcmd:RegisterSlashHandler( "Silent: Toggle output off when blocking people", "^silent$","OutSw")
	self.slashcmd:RegisterSlashHandler( "Message: Sets the message when people bother you, blank disables it and reset resets it to default", "^message (.+)$","SetMessage")
	self.slashcmd:RegisterSlashHandler( "List: Lits the current settings", "^list$","Settings")
	self:RegisterEvent("DUEL_REQUESTED")
	self:RegisterEvent("PARTY_INVITE_REQUEST")
	self:RegisterEvent("TRADE_SHOW")
	self:RegisterEvent("FRIENDLIST_UPDATE")
	self:FRIENDLIST_UPDATE()
end

local orig = ChatFrame_MessageEventHandler

function ChatFrame_MessageEventHandler(event, ...)
	if AntiSocial.db.profile.blocks["whisper"] and AntiSocial.db.profile["enabled"] then
		if event == "CHAT_MSG_WHISPER" then
			local player = arg2
			local text = arg1
			if not AntiSocial.db.profile["friends"] and friends[player] then
				orig(event, ...)
			elseif player == UnitName("Player") then
				orig(event, ...)
			elseif not warned[player] and type(AntiSocial.db.profile["message"]) == "string" then
				SendChatMessage(AntiSocial.db.profile["message"] ,"WHISPER", nil, player)
				warned[player] = true
			end
		else
			orig(event, ...)
		end
	else
		orig(event, ...)
	end
end

function AntiSocial:TRADE_SHOW(event, arg1, ...)
	if self.db.profile.blocks["trade"] and self.db.profile["enabled"] then
		HideUIPanel(StaticPopup1)
		CancelTrade()
		self:Out("Blocked a trade")
	end
end

function AntiSocial:DUEL_REQUESTED(event, arg1, ...)
	if event == "DUEL_REQUESTED" and self.db.profile["enabled"] and self.db.profile.blocks["duel"] then
		if self.db.profile["friends"] then
			HideUIPanel(StaticPopup1)
			CancelDuel()
			DoEmote("SHOO", arg1)
			self:Out("Declining: "..arg1)
			if not warned[arg1] and type(self.db.profile["message"]) == "string" then
				SendChatMessage(self.db.profile["message"] ,"WHISPER", nil, arg1)
				warned[arg1] = true
			end
			else
				if not friends[arg1] then
					HideUIPanel(StaticPopup1)
					CancelDuel()
					DoEmote("SHOO", arg1)
					self:Out("Declining: "..arg1)
					if not warned[arg1] and type(self.db.profile["message"]) == "string" then
						SendChatMessage(self.db.profile["message"] ,"WHISPER", nil, arg1)
						warned[arg1] = true
					end
				end
		end
	end
end

function AntiSocial:PARTY_INVITE_REQUEST(event, arg1, ...)
	if event == "PARTY_INVITE_REQUEST" then
		if self.db.profile["enabled"] and self.db.profile.blocks["invite"] then
			if self.db.profile["friends"]  then
				HideUIPanel(StaticPopup1)
				DeclineGroup()
				if not warned[arg1] and type(self.db.profile["message"]) == "string" then
					SendChatMessage(self.db.profile["message"] ,"WHISPER", nil, arg1)
					warned[arg1] = true
				end
			else
				if not friends[arg1] then
					HideUIPanel(StaticPopup1)
					DeclineGroup()
					if not warned[arg1] and type(self.db.profile["message"]) == "string" then
						SendChatMessage(self.db.profile["message"] ,"WHISPER", nil, arg1)
						warned[arg1] = true
					end					
				end
			end
		end
	end
end

function AntiSocial:Out(...)
	if not self.db.profile["silent"] then
		self:Print(...)
	end
end

function AntiSocial:FRIENDLIST_UPDATE()
	for i = 1,GetNumFriends() do
		local name = GetFriendInfo(i)
		friends[name] = true
	end
	for i = 1,GetNumGuildMembers(includeOffline) do
		local name = GetFriendInfo(i)
		friends[name] = true
	end
end

function AntiSocial:BlockFriends()
	if self.db.profile["friends"] == true then
		self.db.profile["friends"] = false
		self:Print("No longer blocking friends")
	else
		self.db.profile["friends"] = true
		self:Print("Now blocking friends")
	end
end

function AntiSocial:EnableSw()
	if self.db.profile["enabled"] then
		self.db.profile["enabled"] = false
		self:Print("Disabled")
		self:Disable()
	else
		self.db.profile["enabled"] = true
		self:Print("Enabled")
		self:Enable()
	end
end

function AntiSocial:TradeSw()
	if self.db.profile.blocks["trade"] then
		self.db.profile.blocks["trade"] = false
		self:Print("Allowing trades")
	else
		self.db.profile.blocks["trade"] = true
		self:Print("Blocking Trades")
	end
end
function AntiSocial:OutSw()
	if self.db.profile["silent"] then
		self.db.profile["silent"] = false
		self:Print("Silent mode Disabled")
	else
		self.db.profile["silent"] = true
		self:Print("Silent mode Enabled")
	end
end

function AntiSocial:Settings()
	self:Print("Current blocks:")
		for k,v in pairs(self.db.profile.blocks) do
			self:Print(tostring(k).." = "..tostring(v))
		end
	self:Print("Other settings:")
	self:Print("Silent = "..tostring(self.db.profile["silent"]))
	self:Print("Enabled = "..tostring(self.db.profile["enabled"]))
	self:Print("Friends = "..tostring(self.db.profile["friends"]))
	self:Print("Message = "..tostring(self.db.profile["message"]))
end

--[[based on shutup"s, by Rabbit, slash commands]]
function AntiSocial:SetMessage(text)
	if type(text) ~= "string" or text:trim():len() < 1 then
		self.db.profile["message"] = nil
		self:Print("Mesage disabled")
	elseif strtrim(text) == "reset" then 
		self.db.profile["message"] = "Im AntiSocial, go away."
	else
		self.db.profile["message"]  = tostring(strtrim(text))
		self:Print("Message set to: "..tostring(strtrim(text)))
	end
end